from google.appengine.ext import bulkload
from google.appengine.api import datastore_types
from google.appengine.ext import search
from pyth.inventionmodels import *

class ThemeLoader(bulkload.Loader):
  def __init__(self):
    # Our 'Person' entity contains a name string and an email
    #print 'here'
    bulkload.Loader.__init__(self, 'Theme', 
                         [('name', str),
                          ('workshopIds' , lambda x: x.split(', ')),
                          ('image', lambda x: 'themes/'+x) 
                          ])

  def HandleEntity(self, entity):
    theme = Theme()
    theme.name = entity['name']
    theme.workshopIds = entity['workshopIds']
    theme.image = entity['image']
    
    # not sure what to do if multiple copies already exist
    exists = Theme.gql("WHERE name = :1", theme.name)
    if exists.count() == 0 or (exists.count() == 1 and exists[0].datetime < theme.datetime):
        theme.put()
        for id in theme.workshopIds:
            workshops = Workshop.gql("WHERE recordId = :1", id)
            ThemeRelationalEntry(theme = theme, workshop = workshops[0]).put()
            
    return None

if __name__ == '__main__':
  bulkload.main(ThemeLoader())